'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 铁匠铺
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var modelUtil = require('../util/modelUtil');
var mongoConnector = require('../libs/mongodb/mongoConnector');
/* jshint unused:false */
var smithySchema = require('../schemas/bag/smithySchema.js');
var Smithy = mongoConnector().model('Smithy');
var constantUtil = require('../util/constantUtil');
var PlayerManager = require('../libs/playerManager');
var techType = require('../consts/techType.js');
var unionGroupModel = require('../models/unionGroupModel.js');
var unionTechService = require('../services/unionTechService.js');
var unionTechType = require('../consts/unionTechType.js');

/**
 * 铁匠铺
 */
module.exports.getByRoleId = function (role_id, cb) {
    modelUtil.getByRoleId(Smithy, role_id, function (err, smithy_model) {
        if (!!err) {
            console.error(err);
            return cb(err);
        }
        _resetCurrentEnergy(smithy_model, function (err, model) {
            cb(null, smithy_model);
        });
        //cb(null, smithy_model);
    });
};

/**
 * 刷新数据
 */
module.exports.onRefreshData = function (self, cb) {
    _resetCurrentEnergy(self, cb);
};

/**
 * 精力回复
 * @param  {[type]} smithy_model [description]
 * @return {[type]}            [description]
 */
var _resetCurrentEnergy = function (smithy_model, next) {
    var role_id = smithy_model.role_id;
    var player_manager = smithy_model.mgr;
    var union_group_model;

    if (!player_manager) {
        player_manager = new PlayerManager(role_id);
        player_manager.smithy = smithy_model;
    }

    var recover_seconds = moment().diff(moment(smithy_model.last_recover_time), 's');
    var energy_recover_time = constantUtil.value('energy_recover_time');
    if (recover_seconds < energy_recover_time) {
        return next();
    }

    async.waterfall(
        [

            function (cb) {
                player_manager.getModel(
                    ['role', 'tech'],
                    cb
                );
            },
            function (cb) {
                var union_id = player_manager.role.union_id;
                if (!!union_id) {
                    unionGroupModel.getById(union_id, function (err, union_model) {
                        if (!!err) {
                            console.error('err------------union mgr not ', err);
                        }
                        union_group_model = union_model;
                        cb();
                    });
                } else {
                    cb();
                }
            },
            function (cb) {
                var tech_model = player_manager.tech;
                var tech_value = tech_model.getEffByTechType(techType.TECH_TYPE_90);
                var union_tech_value = unionTechService.getUnionTechEff(union_group_model, unionTechType.UNION_TECH_103);
                var max_energy = constantUtil.value('energy_max_limit') + tech_value + union_tech_value;

                if (smithy_model.max_energy !== max_energy) {
                    smithy_model.max_energy = max_energy;
                }

                var curr_energy = smithy_model.getEnergy();
                if (curr_energy >= max_energy) {
                    return next();
                }

                var add_energy = Math.floor(recover_seconds / energy_recover_time);

                if (add_energy > max_energy - curr_energy) {
                    add_energy = max_energy - curr_energy;
                }

                smithy_model.addEnergy(add_energy);
                if (smithy_model.getEnergy() >= max_energy) {
                    smithy_model.last_recover_time = moment();
                } else {
                    smithy_model.last_recover_time = moment(smithy_model.last_recover_time).add(energy_recover_time * add_energy, 's');
                }

                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                next();
            }
            if (!!union_group_model) {
                union_group_model.save();
            }
            next();
        }
    );
};
